<html>
<head><meta charset="utf-8"><title>Unions with padding · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html">Unions with padding</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="169418603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169418603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169418603">(Jul 01 2019 at 18:20)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> why are you so concerned about giving <code>#[repr(transparent)]</code> unions padding?</p>



<a name="169418666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169418666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169418666">(Jul 01 2019 at 18:21)</a>:</h4>
<p>AFAICT, writing and reading to the padding would be ok - the expectation that the padding does not to change on, e.g., move/copy/etc. would be incorrect under that model, but I don't follow why that is a big deal</p>



<a name="169418717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169418717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169418717">(Jul 01 2019 at 18:22)</a>:</h4>
<p>I was just saying that in case it turns out that we have vectors with padding at the end one day or so</p>



<a name="169418742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169418742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169418742">(Jul 01 2019 at 18:22)</a>:</h4>
<p>we could still have both a "bags of bits" semantics <em>and</em> a vector ABI for unions, as long as the bag of bits is just the part "covered" by any union fields</p>



<a name="169418761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169418761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169418761">(Jul 01 2019 at 18:22)</a>:</h4>
<p>Yeah, that's also a case, but I meant also in the middle, e.g., <code>#[repr(transparent)] union U { x: (u8, u32) }</code>having 3 padding bytes between the u8 and the u32 that are not preserved on move / copy / etc.</p>



<a name="169418765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169418765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169418765">(Jul 01 2019 at 18:23)</a>:</h4>
<p>or maybe we just allow uninitalized integers, and then there's no need to use <code>MaybeUninit</code> for vector types :P</p>



<a name="169418786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169418786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169418786">(Jul 01 2019 at 18:23)</a>:</h4>
<blockquote>
<p>Yeah, that's also a case, but I meant also in the middle, e.g., <code>#[repr(transparent)] union U { x: (u8, u32) }</code>having 3 padding bytes between the u8 and the u32 that are not preserved on move / copy / etc.</p>
</blockquote>
<p>that's just so much harder to specify, now you have a "bag with gaps"</p>



<a name="169418807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169418807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169418807">(Jul 01 2019 at 18:24)</a>:</h4>
<p>how much harder?</p>



<a name="169418908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169418908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169418908">(Jul 01 2019 at 18:24)</a>:</h4>
<p>I dont know any unit I could use to quantify this :P</p>



<a name="169418919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169418919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169418919">(Jul 01 2019 at 18:24)</a>:</h4>
<p>the proposed way of computing the padding is straight forward</p>



<a name="169419078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169419078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169419078">(Jul 01 2019 at 18:26)</a>:</h4>
<p>and there is also the cost this has, as we have seen with your ctest problem</p>



<a name="169419091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169419091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169419091">(Jul 01 2019 at 18:26)</a>:</h4>
<p>or would fixing this by not making the union "transparent" not help?</p>



<a name="169419171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169419171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169419171">(Jul 01 2019 at 18:27)</a>:</h4>
<p>I mean, my ctest problem was me assuming that repr(transparent) unions wouldn't have any padding</p>



<a name="169419254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169419254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169419254">(Jul 01 2019 at 18:28)</a>:</h4>
<p>I thought that is how repr(Rust) and repr(transparent) unions were implemented, but it turns out this was not the case</p>



<a name="169419275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169419275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169419275">(Jul 01 2019 at 18:28)</a>:</h4>
<p>so using <code>repr(C)</code> to kill the padding would help? or not?</p>



<a name="169419365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169419365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169419365">(Jul 01 2019 at 18:29)</a>:</h4>
<p><code>repr(C)</code> has padding</p>



<a name="169419474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169419474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169419474">(Jul 01 2019 at 18:30)</a>:</h4>
<p>as in, trailing padding, for example</p>



<a name="169419517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169419517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169419517">(Jul 01 2019 at 18:31)</a>:</h4>
<p>C does not require copies to preserve the padding bytes, C++ does (EDIT: for unions)</p>



<a name="169419640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169419640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169419640">(Jul 01 2019 at 18:32)</a>:</h4>
<p><a href="http://port70.net/~nsz/c/c11/n1570.html#note51" target="_blank" title="http://port70.net/~nsz/c/c11/n1570.html#note51">http://port70.net/~nsz/c/c11/n1570.html#note51</a></p>



<a name="169419810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169419810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169419810">(Jul 01 2019 at 18:34)</a>:</h4>
<p>If we also require copying all union bytes, then we can do a roundtrip with C++ - with C we can't do it anyways.</p>



<a name="169420008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169420008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169420008">(Jul 01 2019 at 18:37)</a>:</h4>
<p>oh so trailing padding is already a problem for you</p>



<a name="169420032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169420032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169420032">(Jul 01 2019 at 18:37)</a>:</h4>
<p>well yes the entire point of this discussion was to require copying all unions bytes right? or as many as possible anyway^^</p>



<a name="169420078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169420078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169420078">(Jul 01 2019 at 18:38)</a>:</h4>
<p>like, say, max-size-of-a-field many bytes</p>



<a name="169425258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169425258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169425258">(Jul 01 2019 at 19:46)</a>:</h4>
<p>the question is, if all fields of the union have a whole in the same bit offset, why copy that on copy ? That hole is not observable through any field access.</p>



<a name="169426273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169426273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169426273">(Jul 01 2019 at 20:00)</a>:</h4>
<p>this assumes the union is complete</p>



<a name="169426297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169426297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169426297">(Jul 01 2019 at 20:00)</a>:</h4>
<p>at least in C land, it is not uncommon to consider adding a new variant to a union a backwards compatible change</p>



<a name="169426328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169426328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169426328">(Jul 01 2019 at 20:01)</a>:</h4>
<p>keeps the library extensible with new options that old clients can just forward, or if they have to handle them error because the tag they see is unknown</p>



<a name="169429867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions%20with%20padding/near/169429867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Unions.20with.20padding.html#169429867">(Jul 01 2019 at 20:46)</a>:</h4>
<p>good point</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>